module.exports = function ($scope, $modalInstance, $resource, $modal, item, flag, toaster){
    

       
        init();
        function init(){
            $scope.citycode = flag ? {'flag': true} : angular.copy(item);
            $scope.have = flag ? {'flag': true} : angular.copy(item);
            $scope.address_list = [];
            if( $scope.citycode !== undefined){
                var arr= $scope.citycode.address.split(',');
                for(var i = 0; i < arr.length ; i++){
                    $scope.address_list.push({'newaddress': arr[i]});
                }
            }
           
        }
        
        //保存
        $scope.save = function (){
            var alladdress = '';
            for(var i = 0; i < $scope.address_list.length; i++){
                if(i == 0){
                    alladdress = $scope.address_list[i].newaddress;
                }else{
                    alladdress = alladdress + ',' + $scope.address_list[i].newaddress;
                }
            }
            var para = {
                'code': $scope.citycode.code,
                'address': alladdress
            }
            var url = '/api/ac/puc/addressCodeService/save';
            $resource(url, {}, {}).save(para, function(res){
                if(res.errcode === 0){
                    toaster.success({title: '', body: '保存成功'});
                    $modalInstance.close();
                }else{
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }

         //添加城市地址
        $scope.add = function () {
            for(var i = 0; i < $scope.address_list.length; i++){
                if($scope.citycode.newaddress == $scope.address_list[i].newaddress){
                    toaster.error({title: '', body: '不能重复添加'});
                    return;
                }
            }
            var temp = {
                'newaddress': $scope.citycode.newaddress
            };
            $scope.citycode.newaddress = '';
        $scope.address_list.push(temp);
        }
    
        //删除已添加城市地址
        $scope.remove = function (index) {
            $scope.address_list.splice(index,1)
        };

        //取消
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        }
    
    };